from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

from options import options


class Asms():
    def __init__(self, driver):
        self.driver = driver

    # 登录
    def login(self, id, userame, passwoed, vericode):
        self.driver.find_element(By.ID, 'taskId').clear()
        self.driver.find_element(By.ID, 'taskId').send_keys(id)
        self.driver.find_element(By.ID, 'loginName').clear()
        self.driver.find_element(By.ID, 'loginName').send_keys(userame)
        self.driver.find_element(By.ID, 'password').clear()
        self.driver.find_element(By.ID, 'password').send_keys(passwoed)
        self.driver.find_element(By.ID, 'vericode').clear()
        self.driver.find_element(By.ID, 'vericode').send_keys(vericode)
        self.driver.find_element(By.XPATH, '//*[@id="fmedit"]/div[2]/div[6]/input').click()
    def modify_password(self):
        pass
    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()

    # 修改电话
    def modify_tel(self, id, username, password, vericode, tel):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.ID, 'phone').clear()
        self.driver.find_element(By.ID, 'phone').send_keys(tel)
        self.driver.find_element(By.LINK_TEXT, '保存').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(2)

    # 资产新增
    def add_asserts(self, id, username, password, vericode, name):
        self.login(id, username, password, vericode)
        # 跳转到资产管理
        self.driver.find_element(By.LINK_TEXT, '资产管理').click()
        self.driver.find_element(By.CLASS_NAME, 'button_true').click()
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(name)
        ele1 = self.driver.find_element(By.ID, 'providerId')
        Select(ele1).select_by_index(2)
        sleep(2)
        ele2 = self.driver.find_element(By.ID, 'storageId')
        Select(ele2).select_by_index(3)
        sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]/div/div[1]').click()
        sleep(3)
        self.driver.switch_to.alert.accept()

    # 资产修改
    def modify_assert(self, id, username, password, vericode, name):
        self.login(id, username, password, vericode)
        # 跳转到资产管理
        self.driver.find_element(By.LINK_TEXT, '资产管理').click()
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[1]').click()
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(name)
        ele1 = self.driver.find_element(By.ID, 'storageId')
        Select(ele1).select_by_index(2)
        sleep(2)
        ele2 = self.driver.find_element(By.ID, 'providerId')
        Select(ele2).select_by_index(3)
        sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]').click()
        sleep(3)
        self.driver.switch_to.alert.accept()

    # 关键字查询资产
    def search_assert(self, id, username, password, vericode, kw):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT, '资产管理').click()
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(kw)
        self.driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div[3]/button').click()

    # 下拉框查询资产
    def select_assert(self, id, username, password, vericode):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT, '资产管理').click()
        ele = self.driver.find_element(By.ID, 'status')
        # #全部状态
        # Select(ele).select_by_index(0)
        # 使用状态
        # Select(ele).select_by_index(1)
        # #未使用状态
        Select(ele).select_by_index(2)
        self.driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div[3]/button').click()
    # 资产借用
    def borrow_assert(self, id, username, password, vericode,num,text):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT,'资产管理').click()
        self.driver.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[6]/td[8]/a[2]').click()
        sleep(2)
        ele = self.driver.find_element(By.ID,'departId')
        Select(ele).select_by_index(num)
        self.driver.find_element(By.ID,'remark').send_keys(text)
        self.driver.find_element(By.XPATH,'//*[@id="fm_new"]/div/div[1]').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
    # 归还资产
    def return_assert(self,id, username, password, vericode):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT,'资产管理').click()
        sleep(2)
        self.driver.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[2]').click()
        sleep(2)
        self.driver.find_element(By.XPATH,'//*[@id="cboxLoadedContent"]/div/div/div[2]/div[2]/div[1]').click()

        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(2)
    # 关键字查询资产
    def search_storage(self, id, username, password, vericode, kw):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT, '存放地点').click()
        sleep(2)
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(kw)
        self.driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div[3]/input').click()
        sleep(2)

    def select_storage(self, id, username, password, vericode):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT, '存放地点').click()
        sleep(2)
        ele = self.driver.find_element(By.ID, 'status')
        # #全部状态
        # Select(ele).select_by_index(0)
        # 使用状态
        # Select(ele).select_by_index(1)
        # #废弃状态
        Select(ele).select_by_index(2)
        self.driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div[3]/input').click()
        sleep(2)

    # 关键字查询资产
    def search_provider(self, id, username, password, vericode, kw):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT, '供应商').click()
        sleep(2)
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(kw)
        sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div[3]/input').click()
        sleep(2)

    def select_provider(self, id, username, password, vericode):
        self.login(id, username, password, vericode)
        self.driver.find_element(By.LINK_TEXT, '供应商').click()
        sleep(2)
        ele = self.driver.find_element(By.CLASS_NAME, 'select_gry')
        # #全部状态
        # Select(ele).select_by_index(0)
        # 正常状态
        # Select(ele).select_by_index(1)
        # #废弃状态
        Select(ele).select_by_index(2)
        self.driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div[3]/input').click()
        sleep(2)


if __name__ == '__main__':
    url = r'http://172.16.22.105:8080/bsams/front/login.do?'
    id = 1
    username = 'test01'
    password = '123456'
    vericode = 'shtd'
    tel = 1234567890
    name = '海尔空调'
    new_name = '海尔冰箱'
    kw = '海尔'
    storage_kw = '电脑'
    provider_kw = '北京'
    num = 2
    text = '这是一次友好的借用'
    driver = webdriver.Chrome(options=options)
    driver.get(url)
    driver.maximize_window()

    asms = Asms(driver)
    # 测试登录
    # asms.login(id,username,password,vericode)
    # sleep(3)
    # 测试退出
    # asms.logout()
    # sleep(2)
    # 测试修改电话号码
    # asms.modify_tel(id, username, password, vericode, tel)
    # sleep(3)
    # 测试新增资产
    # asms.add_asserts(id, username, password, vericode, name)
    # sleep(2)
    # # 测试关键字查询资产
    # asms.search_assert(id, username, password, vericode, kw)
    # sleep(2)
    # #测试下拉查询资产
    # asms.select_assert(id, username, password, vericode)
    # sleep(3)
    # 测试下拉查询存放地点
    # asms.select_storage(id, username, password, vericode)
    # sleep(3)
    # # 测试关键字查询存放地点
    # asms.search_storage(id, username, password, vericode,storage_kw)
    # sleep(2)
    # # 测试下拉查询供应商
    # asms.select_provider(id, username, password, vericode)
    # sleep(3)
    # # 测试关键字查询供应商
    # asms.search_provider(id, username, password, vericode, provider_kw)
    # sleep(2)
    # # 测试修改资产
    # asms.modify_assert(id, username, password, vericode,new_name)
    # sleep(2)
    # # 测试资产借用
    # asms.borrow_assert(id, username, password, vericode,num,text)
    # sleep(2)
    # 测试资产归还
    asms.return_assert(id, username, password, vericode)
    sleep(2)